package com.tnovoselec.android.ui.splash;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public abstract class SplashScreenFragment extends DialogFragment implements OnClickListener {

    public abstract int getContentViewId();

    public abstract long getDuration();

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setStyle(STYLE_NO_FRAME, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
	if (getDuration() > 0 && handler != null) {
	    handler.sendEmptyMessageDelayed(0, this.getDuration());
	}
    }

    @Override
    public void onStop() {
	super.onStop();
	handler.removeMessages(0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View root = inflater.inflate(getContentViewId(), container, false);
	root.setOnClickListener(this);
	return root;
    }

    @Override
    public void onClick(View v) {
	if (handler != null) {
	    handler.removeMessages(0);
	}
	dismiss();
    }

    private Handler handler = new Handler() {
	public void handleMessage(Message msg) {
	    dismiss();
	};
    };

}
